package com.zx.map.controller;

import com.zx.map.dao.model.BaseAcctInfo;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.domain.BaseAcctQueryDomain;
import com.zx.platform.utils.PmpUtils;
import com.zx.platform.utils.report.ReportUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by IntelliJ IDEA. User: liubaojun Date: 15-05-12 Time: 下午12:00
 * 商户信息处理.
 */
@Controller
@RequestMapping("/manage/baseAcctInfo")
public class BaseAcctInfoController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(BaseAcctInfoController.class);

	@Autowired
	private BaseAcctInfoService baseAcctInfoService;


	/**
	 * 查询商户列表
	 *
	 * @param model
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryAcctInfoList")
	public String queryAcctInfoList(BaseAcctQueryDomain domain, Model model,
	                                HttpServletRequest request) {
		// 初始化信息
		try {
			BaseAcctInfoModel baseAcctInfo = new BaseAcctInfoModel();
			MerchantInfoModel searchMerchantInfoModel = new MerchantInfoModel();
			initQueryData(domain, baseAcctInfo, searchMerchantInfoModel);
			//设置查询参数
			int pageSize = domain.getPageSize();
			int pageNum = domain.getPageNum();
			// 查询
			Map map = baseAcctInfoService.queryPageByDomain(searchMerchantInfoModel, baseAcctInfo, pageSize, pageNum);
			setResultData(map, domain, model);

			return "/jsp/map/baseacct/baseAcctList";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/error";
		}
	}


	/**
	 * 下载账户余额查询
	 *
	 * @param domain
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/downloadAcctInfoList")
	public String downloadAcctInfoList(BaseAcctQueryDomain domain, Model model,
	                                   HttpServletRequest request, HttpServletResponse response) {
		try {
			BaseAcctInfoModel baseAcctInfo = new BaseAcctInfoModel();
			MerchantInfoModel searchMerchantInfoModel = new MerchantInfoModel();
			initQueryData(domain, baseAcctInfo, searchMerchantInfoModel);
			//设置查询参数
			int pageSize = SUConstants.PAGE_NOT_PAGE_COUNT;
			int pageNum = domain.getPageNum();
			// 查询
			Map map = baseAcctInfoService.queryPageByDomain(searchMerchantInfoModel, baseAcctInfo, pageSize, pageNum);
			downloadAcctList(map, response, "账户余额");
			return null;
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 下载表报
	 * @param map
	 * @param response
	 * @param fileName
	 * @throws Exception
	 */
	private void downloadAcctList(Map map, HttpServletResponse response, String fileName) throws Exception {
		List<BaseAcctInfoModel> reportList = (List<BaseAcctInfoModel>) map.get("list");
		Map<Integer, Object> formatAttr = new HashMap<Integer, Object>();
		formatAttr.put(1, ReportUtil.Date_Time2);
		String[] header = null;
		header = new String[]{"开户时间", "账户", "账户名称", "总金额", "可用余额", "冻结余额"};
		String[] feildName = new String[]{"createDate", "sysAccount", "merchantName",
				"acctBal", "avlBal", "frzBal"};
		String sts = ReportUtil.exportReportXLS(header, reportList, null, fileName, formatAttr, feildName, response);
		String[] st = sts.split("#");
		ReportUtil.download(response, st[0], st[1]);
	}


	/**
	 * 弹出的冻结商户余额
	 *
	 * @param custId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/frzAcctAmt")
	public String frzAcctAmt(String custId, Model model,
	                         HttpServletRequest request) {
		try {
			BaseAcctInfoModel temp = baseAcctInfoService.getModelByKey(custId);
			model.addAttribute("model", temp);
			return "/jsp/map/baseacct/frzAcctAmt";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}

	/**
	 * 弹出的解冻商户余额
	 *
	 * @param custId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/deFrzAcctAmt")
	public String deFrzAcctAmt(String custId, Model model,
	                           HttpServletRequest request) {
		try {
			BaseAcctInfoModel temp = baseAcctInfoService.getModelByKey(custId);
			model.addAttribute("model", temp);
			return "/jsp/map/baseacct/deFrzAcctAmt";
		} catch (Exception e) {
			log.error("内部异常", e);
			model.addAttribute("errorMsg", "出错了");
			return "/jsp/platform/error";
		}
	}


	/**
	 * 更新用户账户余额
	 *
	 * @param custId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateFzrUserAcct")
	@ResponseBody
	public BaseAcctInfoModel updateFzrUserAcct(
			String custId, String frzAmt,
			Model model, HttpServletRequest request) {
		BaseAcctInfoModel temp = new BaseAcctInfoModel();
		try {
			temp = baseAcctInfoService.getModelByKey(custId);
			if (temp.getAvlBal() >= Double.parseDouble(frzAmt)) {
				baseAcctInfoService.frzBaseAcctBalByKey(new BigDecimal(frzAmt), custId);
				temp.setSuccess(true);
				temp.setMsg("冻结成功");
			} else {
				temp.setSuccess(false);
				temp.setMsg("冻结金额超过可用余额");
			}
		} catch (Exception e) {
			log.error("内部异常", e);
			temp.setSuccess(false);
			temp.setMsg("冻结失败");
		}
		return temp;
	}

	/**
	 * 更新用户账户余额
	 *
	 * @param custId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateDeFrzUserAcct")
	@ResponseBody
	public BaseAcctInfoModel updateDeFrzUserAcct(
			String custId, String deFrzAmt,
			Model model, HttpServletRequest request) {
		BaseAcctInfoModel temp = new BaseAcctInfoModel();
		try {
			temp = baseAcctInfoService.getModelByKey(custId);
			if (temp.getFrzBal() >= Double.parseDouble(deFrzAmt)) {
				baseAcctInfoService.deFrzBaseAcctBalByKey(new BigDecimal(deFrzAmt), custId);
				temp.setSuccess(true);
				temp.setMsg("解冻成功");
			} else {
				temp.setSuccess(false);
				temp.setMsg("解冻金额超过冻结金额");
			}
		} catch (Exception e) {
			log.error("内部异常", e);
			temp.setSuccess(false);
			temp.setMsg("解冻失败");
		}
		return temp;
	}


	/**
	 * 设置返回数据
	 *
	 * @param map
	 * @param domain
	 * @param model
	 */
	private void setResultData(Map map, BaseAcctQueryDomain domain, Model model) {
		int pageSize = domain.getPageSize();
		int pageNum = domain.getPageNum();
		List<BaseAcctInfoModel> baseAcctInfoModels = (List<BaseAcctInfoModel>) map.get("list");
		int recordCount = Integer.valueOf(map.get("count").toString());
		// 页面信息

		if (pageSize * pageNum > recordCount + pageSize - 1) {
			pageNum = 1;
		}
		double totalAvlbals = (Double) map.get("totalAvlbal");
		double totalFrzbals = (Double) map.get("totalFrzbal");
		model.addAttribute("list", baseAcctInfoModels);
		model.addAttribute("model", domain);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("pageCount", PmpUtils.getPageCount(recordCount, pageSize));
		model.addAttribute("recordCount", recordCount);
		model.addAttribute("totalAvlbals",totalAvlbals);
		model.addAttribute("totalFrzbals",totalFrzbals);

	}

	/**
	 * 初始化查询数据
	 *
	 * @param domain
	 * @param baseAcctInfo
	 * @param searchMerchantInfoModel
	 */
	private void initQueryData(BaseAcctQueryDomain domain, BaseAcctInfoModel baseAcctInfo, MerchantInfoModel searchMerchantInfoModel) {
		Integer pageNum = domain.getPageNum();
		Integer pageSize = domain.getPageSize();
		Integer pageCount = domain.getPageCount();
		if (null == pageNum) {
			pageNum = 1;
		}
		if (null == pageSize) {
			pageSize = SUConstants.PAGE_COUNT;
		}
		domain.setPageNum(pageNum);
		domain.setPageSize(pageSize);

		searchMerchantInfoModel.setMerchantName(domain.getMerchantName());
		searchMerchantInfoModel.setSysAccount(domain.getSysAccount());

	}

}
